﻿using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using WssEvents;

namespace JQMergeFramework
{

    public class UIEvent : MonoBehaviour
    {
        public Text titleTxt;
        public Text tipsTxt,useResultTips;
        public Transform lightIcon;
        public UISwitchChild switchChild;

        //0直升机，1无人机
        public EventTypes showEvent;

    
        
        
        private void Start()
        {
            UIManager.Instance.AddBtnListener(this.transform);
        }


        public void Open(EventTypes eventTypes)
        {
            showEvent = eventTypes;
            GetComponent<UIPopAnimate>().PopStart();

      
            if (eventTypes == EventTypes.Helicopter)
            {
                if (DataManager.Instance.DayLimitHelicopter > 0)
                {
                    tipsTxt.text = string.Format("该支援每天只能使用<color=#4bbb00>{0}</color>/{1}次",DataManager.Instance.DayLimitHelicopter,GameCfg.DayLimitEvent);
                }
                else
                {
                    tipsTxt.text = string.Format("该支援每天只能使用<color=#fe4a49>{0}</color>/{1}次",DataManager.Instance.DayLimitHelicopter,GameCfg.DayLimitEvent);
                }

                titleTxt.text = "工程直升机";
            }
            else if (eventTypes == EventTypes.UAV)
            {
                if (DataManager.Instance.DayLimitUAV > 0)
                {
                    tipsTxt.text = string.Format("该支援每天只能使用<color=#4bbb00>{0}</color>/{1}次",DataManager.Instance.DayLimitUAV,GameCfg.DayLimitEvent);
                }
                else
                {
                    tipsTxt.text = string.Format("该支援每天只能使用<color=#fe4a49>{0}</color>/{1}次",DataManager.Instance.DayLimitUAV,GameCfg.DayLimitEvent); 
                }
                titleTxt.text = "工程无人机";
            }
            
            MergeManager.Instance.isUseEventUI = true;
            
            lightIcon.transform.DOLocalRotate(new Vector3(0, 0, 360), 5, RotateMode.WorldAxisAdd).SetEase(Ease.Linear)
                .SetLoops(-1, LoopType.Incremental);


            useResultTips.text = showEvent == EventTypes.Helicopter
                ? string.Format("工程直升机会带走已放置最低等\n级的{0}个建筑！", GameCfg.HelicopterRemoveCityNum)
                : string.Format("支援无人机会给予当前最高等级\n的{0}个建筑！", 1);
            switchChild.index = showEvent == EventTypes.Helicopter ? 0:1;
        }



        void OnBtnClick(Transform trans)
        {
            AudioManager.Instance.Play(AudioName.Click);
            
            switch (trans.name)
            {
                case "BtnReceive":
                    //事件触发
#if UNITY_GAME_TEST
                GlobalEvent.Dispatcher(GameEvents.OnTargetEvent,showEvent);                   
#else
                    if (showEvent == EventTypes.UAV)
                    {
                        if (DataManager.Instance.DayLimitUAV<=0)
                        {
                            return;
                        }
                        DataManager.Instance.DayLimitUAV--;
                    }
                    else if (showEvent == EventTypes.Helicopter)
                    {
                        if (DataManager.Instance.DayLimitHelicopter<=0)
                        {
                            return;
                        }
                        DataManager.Instance.DayLimitHelicopter--;
                    }
                    //触发随机事件
                    GlobalEvent.Dispatcher(GameEvents.OnTargetEvent,showEvent);         
#endif
                    Close();
                    break;
                case "BtnClose":
                    //取消使用
                    GlobalEvent.Dispatcher(GameEvents.OnCancelUseEvent,showEvent);
                    Close();
                    break;
            }
        }
        

        void Close()
        {
            MergeManager.Instance.isUseEventUI = false;
            lightIcon.DOKill();
            GetComponent<UIPopAnimate>().PopEnd();
        }
        
    }
}